﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.ComponentModel;
using Farmacie.Managers;

namespace Farmacie.Windows
{
    /// <summary>
    /// Interaction logic for LogIn.xaml
    /// </summary>
    public partial class LogIn : RadWindow, INotifyPropertyChanged
    {
        public string Username { get; set; }
        public string Password { get; set; }

        private string error;
        public string Error
        {
            get { return error; }
            set
            {
                if (value != error)
                {
                    error = value;
                    OnPropertyChanged("Error");
                }
            }
        }

        public LogIn()
        {
            InitializeComponent();
            buttonLogin.Click += new RoutedEventHandler(buttonLogin_Click);
            this.KeyUp += new KeyEventHandler(LogIn_KeyUp);
            this.DataContext = this;
        }

        void LogIn_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TryLogin();
            }
        }

        void buttonLogin_Click(object sender, RoutedEventArgs e)
        {
            TryLogin();
        }

        private void TryLogin()
        {
            Error = string.Empty;
            var success = AuthenticationManager.Instance.Login(Username, Password);

            if (!success)
            {
                Error = @"Atentie! Utilizator sau parola sunt gresite.";
            }
            else
            {
                this.Close();
            }
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion
    }
}
